package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.Finalheadresultinfo;
import com.grad.why.studentsystem.domain.Student;
import com.grad.why.studentsystem.domain.current.CurrentHeadInfos;
import com.grad.why.studentsystem.domain.current.CurrentRuntimeHeadInfos;
import com.grad.why.studentsystem.service.HeadInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/studentManageSystem")
@Controller
public class HeadInfoController {
    @Autowired
    private HeadInfoService headInfoService;

    @RequestMapping("/getHistoryHeadInfos")
    @ResponseBody
    public Object getStudentList(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search
    ) {
        try {
            List<CurrentHeadInfos> rows = headInfoService.getHistoryHeadInfos(offset, limit,search);
            long total = headInfoService.getHistoryHeadInfosLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteHistoryHeadCheckInfos/{ids}")
    @ResponseBody
    public String deleteHistoryHeadCheckInfos(@PathVariable("ids") String ids){
        try{
            String[] idArrays = ids.split("-");
            for(String id : idArrays){
                int n = headInfoService.deleteHistoryInfoById(Long.parseLong(id));
                if(n<=0){
                    return null;
                }
            }
            return "OK";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/getRuntimeHeadInfos")
    @ResponseBody
    public Object getRuntimeHeadInfosList(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search
    ) {
        try {
            List<CurrentRuntimeHeadInfos> rows = headInfoService.getRuntimeHeadInfos(offset, limit,search);
            long total = headInfoService.getRuntimeHeadInfosLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
